
#foreach ($diagram in $Diagram)
#set($eList = $report.getDiagramElements($diagram))

update test.document_template set permissions = '#foreach ($role in $eList)
#if($report.containsStereotype($role, "secuml.role")) 
#foreach ($form in $eList)
#if($report.containsStereotype($form, "secuml.resource")) 
#foreach($attributes in $form.ownedAttribute)
#if($attributes.name == "ID")
#set($formId = $attributes.defaultValue)
#end##if
#set($attributeClass="")
#foreach ($formClass in $eList)
#if($report.containsStereotype($formClass, "secuml.resource"))
#if($attributes.type.name == $formClass.name)
#set($attributeClass = $attributes.type.name)
#end##if
#end##if
#end##foreach
#if($attributeClass!="") 
#set($fieldName = $attributeClass)
#end##if
#if($attributeClass=="")
#set($fieldName = $attributes.name)
#end##if
#set($hasRead = "-")
#set($hasWrite = "-")
#set($hasInsert = "-")
#set($hasDelete= "-")
#foreach($roleAssociationClass in $AssociationClass)
#if($report.containsStereotype($roleAssociationClass, "secuml.permission"))
#if($roleAssociationClass.relatedElement.get(1).name == $role.name || $roleAssociationClass.relatedElement.get(0).name == $role.name) 
#foreach($associationProp in $roleAssociationClass.ownedAttribute)
#foreach($roleRight in $report.getRelationship($associationProp))
#if($attributes.name==$roleRight.target.get(0).name)
#if($associationProp.type.name == "read") 
#set($hasRead="R")
#end##if
#if($associationProp.type.name == "write") 
#set($hasWrite="W")
#end##if
#if($associationProp.type.name == "insert") 
#set($hasInsert="I")
#end##if
#if($associationProp.type.name == "delete") 
#set($hasDelete="D")
#end##if
#end##if
#end##foreach
#end##foreach
#end##if
#end##if
#end##foreach
#if($fieldName != "" && $fieldName != "ID")$role.name<>$fieldName>>$hasRead,$hasWrite,$hasInsert,$hasDelete<break>#end##if
#end##foreach
#end##if
#end##forearch 
#end##if
#end##foreach
' , 
TEMPLATE_XSD='<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
#foreach ($form in $eList)
#if($report.containsStereotype($form, "xml.schema")) 
<xs:element name="$form.name">
	<xs:complexType>
		<xs:sequence>
		#set($i = 0)
		#set($j = 0)
		#foreach($attributes in $form.ownedAttribute)
		#set($isSimple = 1)
		#foreach($types in $eList)
		#if($attributes.type.name==$types.name && $attributes.name!="" && $attributes.name!="ID")
		#set($string = $attributes.multiplicity)
					#set($mult = $string.split("\.."))
					#set($minoccurs="1")
					#set($maxoccurs="1")	
					#set($Integer=0)
					#if($Integer.parseInt($mult.get(0))>0)
						#set($minoccurs="$mult.get(0)")
					#end##if
					#if($mult.get(1)=="*")
						#set($maxoccurs="unbounded")
					#end##if
					
					#if($Integer.parseInt($mult.get(1))>0)
						#set($maxoccurs=$mult.get(1))
					#end##if
		<xs:element name="$attributes.name">			
			<xs:complexType>
				<xs:sequence>
				<xs:element name="$attributes.type.name" minOccurs="$minoccurs" maxOccurs="$maxoccurs">
				<xs:complexType>
				<xs:sequence>
				#foreach($attrib in $types.ownedAttribute)
					#set($string = $attrib.multiplicity)
					#set($mult = $string.split("\.."))
					#set($minoccurs="1")
					#set($maxoccurs="1")	
					#set($Integer=0)
					#if($Integer.parseInt($mult.get(0))>0)
						#set($minoccurs="$mult.get(0)")
					#end##if
					#if($mult.get(1)=="*")
						#set($maxoccurs="unbounded")
					#end##if
					#if($Integer.parseInt($mult.get(1))>0)
						#set($maxoccurs=$mult.get(1))
					#end##if
					<xs:element name="$attrib.name" type="xs:$attrib.type.name.toLowerCase()" minOccurs="$minoccurs" maxOccurs="$maxoccurs"/>
					#end##foreach
				</xs:sequence>
				</xs:complexType>
				</xs:element>
				</xs:sequence>
			</xs:complexType>
		</xs:element>
		#set ($isSimple = 0)
		#end##if
		#end##foreach
		#if($isSimple == 1  && $attributes.name!="" && $attributes.name!="ID")
					#set($string = $attributes.multiplicity)
					#set($mult = $string.split("\.."))
					#set($minoccurs="1")
					#set($maxoccurs="1")	
					#set($Integer=0)
					#if($Integer.parseInt($mult.get(0))>0)
						#set($minoccurs="$mult.get(0)")
					#end##if
					#if($mult.get(1)=="*")
						#set($maxoccurs="unbounded")
					#end##if
					#if($Integer.parseInt($mult.get(1))>0)
						#set($maxoccurs=$mult.get(1))
					#end##if
					#set($simpleComplex=0)
					#if($maxoccurs!="1")
					<xs:element name="$attributes.name" >
					<xs:complexType>
					<xs:sequence>
					<xs:element name="$attributes.name" type="xs:$attributes.type.name.toLowerCase()" minOccurs="$minoccurs" maxOccurs="$maxoccurs"/>
					</xs:sequence>
					</xs:complexType>
					</xs:element>
					#end##if
					#if($maxoccurs=="1")
					
		<xs:element name="$attributes.name" type="xs:$attributes.type.name.toLowerCase()" minOccurs="$minoccurs" maxOccurs="$maxoccurs"/>
		#end##if
		#end##if
		#end##foreach
		</xs:sequence>
	</xs:complexType>
</xs:element>
#end##if
#end##foreach
</xs:schema>'

where id=$formId.value;
#end##foreach
